package com.xmg.day05;

/**
 * @Author: mazhongqing
 * @Date: 2020/10/23 14:12
 */
public class ReverseDemo2 {
    public static void main(String[] args) {
        String str = "abcdefghijklmn";
        System.out.println(rolate(str,4));
    }

    public static String rolate(String s,int leftSize){
        char[] str = s.toCharArray();
        int L = 0;
        int R = str.length-1;
        int lpart = leftSize;
        int rpart = str.length - leftSize;
        int same = Math.min(lpart,rpart);
        int diff = lpart - rpart;
        exchange(str,L,R,same);
        while(diff!=0){
            if(diff>0){//左侧大，变换之后，左侧same固定不动，L变为L+same
                L += same;
                lpart = diff;
            }else{//右侧大，变换之后，右侧same固定不动，R变为R-same
                R -= same;
                rpart = -diff;
            }
            same = Math.min(lpart,rpart);
            diff = lpart-rpart;
            exchange(str,L,R,same);
        }
        return String.valueOf(str);
    }

    public static void exchange(char[] str,int L,int R ,int size){
        int i = R - size +1;
        char tmp = 0;
        while(size-- !=0){
            tmp = str[L];
            str[L++] = str[i];
            str[i++] = tmp;
        }
    }
}
